package com.ankiang.treeprint.fragment.components;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.ankiang.treeprint.R;
import com.ankiang.treeprint.base.BaseFragment;
import com.ankiang.treeprint.fragment.components.viewpager.CardTransformer;
import com.ankiang.treeprint.fragment.util.TRUtils;
import com.ankiang.treeprint.lib.Group;
import com.ankiang.treeprint.lib.annotation.Widget;
import com.ankiang.treeprint.manager.QDDataManager;
import com.ankiang.treeprint.model.TRRedpacketNode;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUIPagerAdapter;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.QMUIViewPager;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author cginechen
 * @date 2017-09-13
 */

@Widget(group = Group.Home, widgetClass = TRRedpacketDistributeFragment.class, iconRes = R.drawable.ic_icon_redpacket)
public class TRRedpacketDistributeFragment extends BaseFragment {
    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;
//    @BindView(R.id.pager)
//    QMUIViewPager mViewPager;

    @BindView(R.id.pull_to_refresh)
    QMUIPullRefreshLayout mPullRefreshLayout;

    @BindView(R.id.redpacket_list)
    ListView listView;

    private List<TRRedpacketNode> mItems = new ArrayList<>();


    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.tr_fragment_redpacket, null);
        ButterKnife.bind(this, root);

        initData();
        initTopBar();


        mPullRefreshLayout.setOnPullListener(new QMUIPullRefreshLayout.OnPullListener() {
            @Override
            public void onMoveTarget(int offset) {

            }

            @Override
            public void onMoveRefreshView(int offset) {

            }

            @Override
            public void onRefresh() {
                updateData();
                mPullRefreshLayout.finishRefresh();
            }
        });
        return root;
    }

    private void initData() {
        int count = 20;
        mItems = new ArrayList<>();
        for (int i = 0; i < count; i++) {

            TRRedpacketNode tmp = new TRRedpacketNode();
            tmp.setId(i);
            tmp.setTitle("test-" + i);
            tmp.setNote("test-" + i);
            tmp.setUpdatetime("2018-02-10 11:30");
            tmp.setAmount(i);
            tmp.setStatus(true);
            mItems.add(tmp);
        }
        updateData();
    }
    private void updateData(){
        Collections.sort(mItems, (a, b)->{
            if(a==b){
                return 0;
            }
            if(a == null){
                return -1;
            }
            if(b == null){
                return 1;
            }

            if(a.getStatus()!=b.getStatus()){
                if(a.getStatus()){
                    return -1; // 在前
                }else{
                    return 1;
                }
            }

            // 后加的在前面
            return b.getId() - a.getId();
        });

        RedpacketListAdapter adapter = new RedpacketListAdapter(getContext());
        adapter.setData(mItems);
        listView.setAdapter(adapter);
        TRUtils.setListViewHeightBasedOnChildren(listView);


//        AdapterView.OnItemClickListener onClickGroupItemListener
        listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

                TRReadpacketAddFragment fragment = new TRReadpacketAddFragment();

                fragment.setOp(TRReadpacketAddFragment.EDIT);
                TRRedpacketNode data = mItems.get(pos);
                Bundle bundle = new Bundle();
                bundle.putSerializable(TRReadpacketAddFragment.KEY_DATA, data);
                fragment.setArguments(bundle);

                fragment.setCallback(o -> {
                    updateData();
                });

                startFragment(fragment);
            }
        });


    }

    private void initTopBar() {
        mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popBackStack();
            }
        });
        mTopBar.setTitle(QDDataManager.getInstance(getContext()).getDescription(this.getClass()).getName());
    }


    public class RedpacketListAdapter extends BaseAdapter {


        private Context context;

        public RedpacketListAdapter(Context context) {
            this.context = context;

        }


        public Context getContext() {
            return context;
        }


        List<TRRedpacketNode> data;

        public void setData(List<TRRedpacketNode> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }


        @Override
        public TRRedpacketNode getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View ll = null;
            if (convertView != null) {
                ll = convertView;
            } else {
                ll = LayoutInflater.from(getContext()).inflate(R.layout.tr_redpacket_view_item, null);
            }

            TRRedpacketNode dt = getItem(position);

            TextView titleTextView = ll.findViewById(R.id.title);
            TextView datetimeTextView = ll.findViewById(R.id.datetime);
            TextView noteTextView = ll.findViewById(R.id.note);
            TextView amountTextView = ll.findViewById(R.id.amount);
            CheckBox statusCheckBox = ll.findViewById(R.id.status);
            titleTextView.setText(dt.getTitle());
            datetimeTextView.setText(dt.getUpdatetime());
            noteTextView.setText(dt.getNote());
            amountTextView.setText(String.valueOf(dt.getAmount()));

            boolean status = dt.getStatus();
            statusCheckBox.setChecked(status);
            if (status) {
                titleTextView.setTextColor(getResources().getColor(R.color.app_color_description));
            }else{
                titleTextView.setTextColor(getResources().getColor(R.color.black));
            }
            statusCheckBox.setOnClickListener(v->{
                if(statusCheckBox.isChecked()){
                    titleTextView.setTextColor(getResources().getColor(R.color.app_color_description));
                    dt.setStatus(true);
                }else{
                    dt.setStatus(false);
                    titleTextView.setTextColor(getResources().getColor(R.color.black));
                }
                updateData();
            });


            return ll;
        }
    }


}
